package com.myproject.mall.user.controller;

import com.myproject.mall.common.result.HttpResult;
import com.myproject.mall.domain.dto.RegistrationFormDTO;
import com.myproject.mall.domain.vo.UserVO;
import com.myproject.mall.user.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Tag(name= "用户登录接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/login")
public class LoginController {

    private final IUserService userService;

    @Operation(summary = "用户密码登录")
    @GetMapping("/password")
    public HttpResult<String> loginByPassword(@RequestParam String phone, @RequestParam String password)
    {
        UserVO user = userService.loginByPassword(phone, password);
        String token = userService.generateToken(user.getId(), user.getUsername(), user.getPhone());
        return HttpResult.success(token);
    }

    @Operation(summary = "用户验证码登录")
    @GetMapping("/code")
    public HttpResult<String> loginByCode(@RequestParam String phone, @RequestParam String code)
    {
        UserVO user = userService.loginByCode(phone, code);
        String token = userService.generateToken(user.getId(), user.getUsername(), user.getPhone());
        return HttpResult.success(token);
    }

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public HttpResult<String> register(@RequestBody RegistrationFormDTO registrationFormDTO)
    {
        UserVO register = userService.register(registrationFormDTO);
        String token = userService.generateToken(register.getId(), register.getUsername(), register.getPhone());
        return HttpResult.success(token);
    }


}
